const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
// const minicss = require('mini-css-extract-plugin');
const webpack = require('webpack');
const {
  CleanWebpackPlugin
} = require('clean-webpack-plugin');

console.log(path.resolve(__dirname, '../src/renderer/entry.js'))

// 手动指定使用什么做模板引擎
module.exports = {
  mode: 'none',
  target: 'electron-renderer',
  devtool: 'inline-source-map',
  devServer: {
    host: '127.0.0.1',
    hot: true,
    contentBase: path.resolve(__dirname, '../build/'),
    port: 5000,
    overlay: {
      errors: true, //错误提示
    },
    open: true,
  },
  watch: true,
  watchOptions: {
    poll: 5,
    aggregateTimeout: 500,
    ignored: /nodes_modules/,
  },
  resolve: {
    alias: {
      '@api': path.resolve(__dirname, `../src/renderer/api`),
      '@app': path.resolve(__dirname, `../src/renderer/app`),
      '@pages': path.resolve(__dirname, `../src/renderer/pages`),
      '@layout': path.resolve(__dirname, `../src/renderer/layout`),
      '@src': path.resolve(__dirname, '../src/renderer'),
      '@components': path.resolve(__dirname, '../src/renderer/components'),
      'layout': path.resolve(__dirname, `../src/renderer/layout/layout.js`),
    }
  },
  entry: {
    preload: path.resolve(__dirname, '../src/renderer/preload.js'),
    renderer: path.resolve(__dirname, '../src/renderer/entry.js'),
    windowMsg: path.resolve(__dirname, '../src/renderer/components/notificationToWindow/index.js'),
  },
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, '../build/renderer')
  },
  plugins: [
    new HtmlWebpackPlugin({
      filename: path.resolve(__dirname, '../build/fileWatcher.html'),
      template: path.resolve(__dirname, `../src/renderer/pages/fileWatcher/render.js`),
      chunks: ['renderer'],
    }),
    new HtmlWebpackPlugin({
      filename: path.resolve(__dirname, '../build/windowMsg.html'),
      template: path.resolve(__dirname, '../src/renderer/components/notificationToWindow/index.html'),
      chunks: ['windowMsg'],
    }),
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin(), //热更新插件
    new CleanWebpackPlugin(),
  ],
  module: {
    rules: [{
      test: /\.css$/,
      use: ['style-loader', 'css-loader']
    }, {
      test: /\.less$/,
      use: ['style-loader', 'css-loader', 'less-loader'],
    }, {
      test: [/\.styl$/, /\.stylus$/],
      use: ['style-loader', 'css-loader', 'stylus-loader']
    }, {
      test: /\.ejs$/,
      use: ['ejs-loader']
    }, {
      test: /\.jade$/,
      use: ['jade-loader']
    }, {
      test: /\.svg$/,
      use: ['file-loader']
    }, {
      test: /\.pug$/,
      use: [{
        loader: 'pug-loader',
        options: {
          pretty: true //输出的html进行格式化，便于修改
        }
      }]
    }]
  },
  externals: {
    clw: 'commonjs2 clw', //不打包 clw 模块，且使用 commentjs2规范 引入
  }
}